import axios from "axios";
import cookie from "js-cookie";
import {Message} from "element-ui";
const service =axios.create({
  baseURL:'http://localhost:8079',
  timeout:100000
})
// 请求拦截器
service.interceptors.request.use(
  config =>{
    // 判断token是否为空
    if(cookie.get('token')){
      config.headers['token'] = cookie.get('token')
    }
    return config
  },
  err =>{
    return Promise.reject(err)
  }
)
//响应拦截器
service.interceptors.response.use(
  res =>{
  //  状态 不是200
    if(res.data.code == 208){
        loginEvent.$emit('loginDialogEvent')
      return
    }else{
      if(res.data.code != 200){
        Message({
          message: res.data.message,
          type: 'error',
          duration: 3000
        })
        return Promise.reject(res.data)
      }
      return res
    }
  },
  error => {
    return Promise.reject(error.response)
  }
)
export default service
